﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2018 Jean-Marc VIGLINO,
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Geoportail isochrone control</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="Add an isochrone control to your map." />
  <meta name="keywords" content="ol3, control, search, routing" />

  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
  <!-- IE 9 bug on ajax tranport  -->
  <!--[if lte IE 9]>
  <script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js'></script>
  <![endif]-->

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>

  <!-- jsts for buffer -->
  <script src="https://unpkg.com/jsts@2.0.2/dist/jsts.min.js"></script>

  <!-- filesaver-js -->
  <script type="text/javascript" src="https://cdn.rawgit.com/eligrey/FileSaver.js/aa9f4e0e/FileSaver.min.js"></script>

  <script type="text/javascript" src="https://cdn.rawgit.com/Viglino/Geoportail-KISS/gh-pages/apikey.js"></script>
  
  <link rel="stylesheet" href="../style.css"/>
  <style>
    .info a img {
      display: inline-block;
      height: 100px;
      max-width: 150px;
      margin: .5em;
    }
  </style>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Geoportail isochrone</h1>
  </a>

  <div class="info">
    <i>ol.control.IsochroneGeoportail</i> 
    <a href="https://geoservices.ign.fr/documentation/geoservices/isochrones.html">Geoportail API</a>.
    <div style="text-align: center">
      <b>In partnership with:</b><br/>
      <a href="https://www.geoportail.gouv.fr/"><img src="https://www.geoportail.gouv.fr/assets/images/logo-geoportail.svg" alt="Geoportail"></a>
      <a href="http://ign.fr"><img src="https://upload.wikimedia.org/wikipedia/fr/thumb/a/a0/IGN_logo_2012.svg/545px-IGN_logo_2012.svg.png" alt="IGN" class="jsx-3503728609 logo"></a>
    </div>	
    <ul>
      <li>
        You need an API key (<a href="http://professionnels.ign.fr/ign/contrats">get a key</a> 
        see <a href="https://geoservices.ign.fr/documentation/geoservices/isochrones.html">documentation page</a>).
      </li>
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width:100%; height:400px;"></div>

  <div class="options">
    <i>Use the search control to start a new search.</i>
    <ul>
      <li>
        Simplify: <input type="number" class="simplify" value=0 />
      </li>
      <li>
        <button onclick="save()">export</button>
      </li>
    </ul>
  </div>
  
<script type="text/javascript">
  // Layers
  var layers = [ new ol.layer.Tile({ source: new ol.source.Stamen({ layer: 'watercolor' }) }) ];

  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View ({
      zoom: 5,
      center: [166326, 5992663]
    }),
    interactions: ol.interaction.defaults({ altShiftDragRotate:false, pinchRotate:false }),
    layers: layers
  });

  // Set the control grid reference
  var search = new ol.control.IsochroneGeoportail({
    apiKey: apiKey
  });
  map.addControl (search);
  
  var vector = new ol.layer.Vector({
    source: new ol.source.Vector()
  });
  map.addLayer(vector);

  search.on('isochrone', function(iso) {
    if (!batch.length && iso.iteration === 0) vector.getSource().clear();
    if (Number($('.simplify').val())) {
      // Simplify using closing operation with jsts
      var simpli = Number($('.simplify').val());
      var parser = new jsts.io.OL3Parser();
      var geom = iso.feature.getGeometry();
      var jstsGeom = parser.read(geom);
      var buffered = jstsGeom.buffer(simpli);
      buffered = buffered.buffer(-1*simpli);
      buffered = jsts.simplify.DouglasPeuckerSimplifier.simplify(buffered, Math.min(10, simpli/10))
      iso.feature.setGeometry(parser.write(buffered));
    }
    vector.getSource().addFeature(iso.feature);
    setTimeout(function() {
      map.getView().fit(vector.getSource().getExtent(), { duration:1000 });
    }, 500);
    // Next batch
    if (batch.length) dosearch();
  });

  function save () {
    var format = new ol.format.GeoJSON();
    var data = format.writeFeatures(vector.getSource().getFeatures(), {
      dataProjection: 'EPSG:4326',
      featureProjection: map.getView().getProjection()
    })
    var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
    saveAs(blob, "isochrone.geojson");
  }

  /* BATCH mode: load a geojson file */
  var batch = [];
  var current_iteration = 0;
  var dialog = new ol.control.Dialog();
  map.addControl(dialog);

  function dosearch() {
    if (!batch.length) {
      dialog.hide();
      return;
    }
    current_iteration--;
    if (current_iteration<=0) {
      current_iteration = search.get('iter')+1;
      batch.shift();
      dosearch();
      return;
    }
    dialog.show('Calculating: '+batch.length+'/'+current_iteration)
    var pt = batch[0].getGeometry().getFirstCoordinate();
    var val = search.get('method')==='distance' ? search.get('distance')*1000 : (search.get('hour')||0)*60*60+(search.get('minute')||0)*60;
    if (val) {
      search.search(pt, val, current_iteration-1)
    } else {
      batch = [];
      dialog.hide();
    }
  };

  var drop = new ol.interaction.DropFile();
  map.addInteraction(drop);
  drop.on('addfeatures', function(e) {
    console.log(e.features);
    vector.getSource().clear();
    batch = e.features;
    current_iteration = search.get('iter')+1;
    dosearch();
  });
</script>
  
</body>
</html>